//
//  HCAppointmentViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCAppointmentViewController.h"

#import "HCAppointView.h"

#import "HCLocationModel.h"

#import "HCAppointmentModel.h"

#import "HCLogViewController.h"


@interface HCAppointmentViewController () <HCAppointViewDelegate,HCLocationDelegate> {
    HCAppointmentModel *app_model;
    HCAppointView *app;
    
    NSString *lng_ll;
    NSString *lat_ll;
    NSString *city_ll;
    
    NSArray *tableViews;
    
}

@end

@implementation HCAppointmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textTitle = @"预约";
    //初始化页面
    [self loadContentView];

    self.dataRequest.delegate = self;
    
    HCLocationModel *model = [[HCLocationModel alloc] init];
    model.delegate = self;
    
}

-(void)hc_appointmentLat:(NSString *)lat longitude:(NSString *)lng city:(NSString *)city {
    NSLog(@"预约数据:%@,%@,%@",lat,lng,city);

    
//    if ([PublicFunction isNull:HCGETUSER(@"userid")]) {
//        HCLogViewController *log = [[HCLogViewController alloc] init];
//        
//        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:log];
//        na.navigationBarHidden = YES;
//        [self.navigationController presentViewController:na animated:YES completion:nil];
//    }else{
    lng_ll = [NSString stringWithFormat:@"%@",lng];
    lat_ll = [NSString stringWithFormat:@"%@",lat];
    city_ll = [NSString stringWithFormat:@"%@",city];
        [self.dataRequest postLocationAppointment:city userID:HCGETUSER(@"userid") city_lat:lng city_lng:lat];
//    }
    
}

#pragma mark 网络请求

-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    app_model = [[HCAppointmentModel alloc] initWithDictionary:dictionary error:nil];
    app.model = app_model;
    
    if (tableViews) {
        NSLog(@"刷新成功");
        for (UITableView *tab in tableViews) {
            [tab reloadData];
            if (tab.mj_header.state == MJRefreshStateRefreshing) {
                [tab.mj_header endRefreshing];
            }
        }
    }
    
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"数据请求失败，预约");
}


-(void)loadContentView {
    
    app = [[HCAppointView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    app.delegate = self;
    [self.centerView addSubview:app];
}

#pragma mark HCAppointViewDelegate
-(void)appointIndexClicked:(NSIndexPath *)indexPath andDate:(NSString *)date{
    
    HCAppointmentData *data = app_model.index[indexPath.row];
    
    [self pushChildrenViewController:@"HCADetailViewController" parameterObject:data.hc_id dataObject:date];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 刷新控件
-(void)appointmentViewEndRefresh:(NSArray *)tableView {
    
    tableViews = [NSArray arrayWithArray:tableView];
    
    if ([PublicFunction isNull:city_ll]) {
        return;
    }
    
    if ([PublicFunction isNull:lng_ll]) {
        return;
    }
    
    if ([PublicFunction isNull:lat_ll]) {
        return;
    }
    
    [self.dataRequest postLocationAppointment:city_ll userID:HCGETUSER(@"userid") city_lat:lng_ll city_lng:lat_ll];
    

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
